from common import *
from common.manager import Manager
from common.sounds import SoundSystem

class GameManager(Manager):
	"""
	This class is the Game's manager.
	"""
	
	# Constants
	SOUNDS_FOLDER = '../data/sounds/'
	MUSICS_FOLDER = '../data/musics/'
	SOUND_CHANNELS = 6
	
	# Possible game states
	LOADING = 0
	MAIN_MENU = 1
	PLAYING = 2
	PAUSED = 3
	FINISHED = 4
	
	# Attributes
	main_window = None
	state = LOADING
	terrain = None
	hovered = None
	selected = None
	targeted = None
	sound_system = SoundSystem(SOUND_CHANNELS)
	
	@staticmethod
	def initialize(map):
		Manager.initialize(map)
		GameManager.load_sounds()
		GameManager.load_musics()
	
	@staticmethod
	def load_sounds():
		GameManager.__load_sound('menu_click', 'robot_activate.mp3')
		GameManager.__load_sound('walk_click', 'robot_activate.mp3')
		GameManager.__load_sound('shot_click', 'LaserWeapon_01.mp3')
		GameManager.__load_sound('shot_damage', 'AlienGrowl_02.mp3')
		
	@staticmethod	
	def load_musics():
		GameManager.__load_music('main_menu', 'main_menu.mp3')
		GameManager.__load_music('game', 'game.mp3')
	
	@staticmethod
	def __load_sound(name, file, folder=None):
		if folder is None:
			folder = GameManager.SOUNDS_FOLDER
		GameManager.sound_system.load(name, folder + file)
		
	@staticmethod
	def __load_music(name, file, folder=None):
		if folder is None:
			folder = GameManager.MUSICS_FOLDER
		GameManager.sound_system.load_stream(name, folder + file)
	
	@staticmethod
	def play_sound(name):
		GameManager.sound_system.volume(1.0)
		GameManager.sound_system.play(name)
	
	@staticmethod	
	def play_music(name):
		GameManager.sound_system.volume(0.7)
		GameManager.sound_system.play_loop(name)
		
	@staticmethod
	def change_state(newState):
		"""
		Changes the game state to a new one.
		"""
		
		if GameManager.state == newState:
			return
		
		GameManager.hovered = None
		GameManager.selected = None
		
		GameManager.state = newState
		GameManager.main_window.change_mode(newState)